package com.elink.enginerring.news;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.Map;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.elink.enginerring.common.Constant;
import com.elink.enginerring.common.ErrorType;
import com.elink.enginerring.datasource.DataSourceContextHolder;
import com.elink.enginerring.datasource.DataSourceType;
import com.elink.enginerring.file.entity.Tag;
import com.elink.enginerring.file.service.ITagService;
import com.elink.enginerring.news.service.INewsTagService;
import com.elink.enginerring.user.entity.User;
import com.elink.enginerring.util.JSONUtil;

/**
 * 新闻标签
 * @author hxjxiaojun
 * @time 2016-9-20
 *
 */
@RestController
@RequestMapping("/news/tag")
public class NewsTagController {
	@Autowired
	private ITagService tagService;
	@Autowired
	private INewsTagService newsTagService;

	/**
	 * 获取标签list
	 * @return
	 */
	@RequestMapping(value = "/getList", method = RequestMethod.GET)
	public Map<String, Object> getList() {
		DataSourceContextHolder.setDbType(DataSourceType.DBNEWS);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		return tagService.getList();
	}

	/**
	 * 添加标签
	 * @param tag
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public Map<String, Object> add(@ModelAttribute @Valid Tag tag) {
		DataSourceContextHolder.setDbType(DataSourceType.DBNEWS);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		return tagService.add(tag);
	}
	
	/**
	 * 修改标签
	 * @param tag
	 * @return
	 */
	@RequestMapping(value = "/modify", method = RequestMethod.POST)
	public Map<String, Object> modify(@ModelAttribute @Valid Tag tag) {
		DataSourceContextHolder.setDbType(DataSourceType.DBNEWS);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		return tagService.modify(tag);
	}
	
	/**
	 * 批量添加标签
	 * @return
	 */
	@RequestMapping(value = "/addBatch", method = RequestMethod.POST)
	@ApiOperation(value="批量添加标签",httpMethod="POST",notes="批量添加标签")
	public Map<String, Object>addBatch(@ApiParam(value="标签集合，以逗号进行分割")
	@RequestParam(value="tags",required=true)String tags,@ApiParam(value="文件主键")
	@RequestParam(value="newsId",required=true)String newsId,HttpSession session){
		DataSourceContextHolder.setDbType(DataSourceType.DBNEWS);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());

		User user=(User) session.getAttribute(Constant.su_in_session);
		String tagIds = 	tagService.addBatch(tags,user.getUserId()).toString();
		//往 new_tag插入数据
		newsTagService.addBatch(tagIds,newsId);
		
		return JSONUtil.getJSON(ErrorType.SUCCESS);
	}
	/**
	 * 删除标签
	 * @param ids
	 * @return
	 */
	@RequestMapping(value = "/del", method = RequestMethod.GET)
	public Map<String, Object> del(@RequestParam("ids") String ids) {
		DataSourceContextHolder.setDbType(DataSourceType.DBNEWS);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		return tagService.del(ids);
	}
	
	/**
	 * 挤压式排序
	 * @param id_seq
	 * @return
	 */
	@RequestMapping(value="/order",method=RequestMethod.GET)
	public Map<String, Object> order(@RequestParam("id_seq") String id_seq){
		
		DataSourceContextHolder.setDbType(DataSourceType.DBNEWS);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		return tagService.order(id_seq);
	}
}
